﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Reflection;

namespace System
{
    public class DALManage<T> where T : BaseModel, new()
    {
        public  static BaseDAL<T> Instance
        {
            get
            {
                return new BaseDAL<T>();
            }
        }

        public  static BaseDAL<T> GetInstance(string configName)
        {
            return new BaseDAL<T>(configName);
        }
    }

    public sealed class DALManager
    {
        private Hashtable objBuffers;

        private static DALManager instance;

        #region

        private DALManager()
        {
            objBuffers = new Hashtable();
        }

        public  static DALManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DALManager();
                }

                return instance;
            }
        }

        #endregion

        public  IBaseDAL<T> CreateMapper<T>(string dalAssemblyName) where T : BaseModel, new()
        {

            string name = typeof(T).Name;
            string objName = name.Substring(0, (name.Length - 4));

            IBaseDAL<T> objDAL;
            string objectFullName = dalAssemblyName + "." + objName;

            if (objBuffers.ContainsKey(objectFullName))
            {
                objDAL = (IBaseDAL<T>)objBuffers[objectFullName];
            }
            else
            {

                objDAL = Assembly.Load(dalAssemblyName).CreateInstance(objectFullName) as IBaseDAL<T>;

                objBuffers.Add(objectFullName, objDAL);
            }

            return objDAL;
        }
    }
}
